//
//  SYAccountLoginViewController.swift
//  SheYangBigData
//
//  Created by 叁拾叁 on 2020/8/20.
//  Copyright © 2020 叁拾叁. All rights reserved.
//

import UIKit
import RxCocoa
import RxSwift
import SnapKit
import Then
import IQKeyboardManagerSwift

class SYAccountLoginViewController: SYBaseViewController, SYLoginProtocol {
    
    
    var loginType: LoginEnum {
        return .account
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.isHidden = true
        IQKeyboardManager.shared.enable = true
        IQKeyboardManager.shared.enableAutoToolbar = true
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.navigationController?.navigationBar.isHidden = false
        IQKeyboardManager.shared.enable = false
        IQKeyboardManager.shared.enableAutoToolbar = false
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        self.navigationController?.navigationBar.isHidden = true
        
        let loginView = SYLoginView(frame: CGRect.zero, loginProtocol: self, viewController: self)
        self.view.addSubview(loginView)
//        loginView.usernameTF.text = "xushusheng"
//        loginView.passwordTF.text = "123456"
        loginView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
    }
    
    
    
    /// SYLoginProtocol
    func thirdLogin() {
        
    }
    
    func login(loginName: String, password: String) {
        
        UserDefaults.standard.setValue(TEST_TOKEN, forKey: "token")
        let token: String = UserDefaults.standard.value(forKey: "token") as? String ?? ""
        if token != "" {
//            let tabbarVC = SYBaseTarBarViewController(roleId: "1")
//            UIApplication.shared.keyWindow?.rootViewController = tabbarVC
//            return
        }
        
        
        requestVM.baseRequest(disposeBag: dispose, type: .login(dic: ["account":loginName,"password": password,"host":"124.71.163.30:10000"]), modelClass: SGBaseModel<NSUserInfoModel>.self) {[weak self] (res) in
            if self != nil {
                let info: NSUserInfoModel  = res.data ?? NSUserInfoModel()
                
                let defaults = UserDefaults.standard
          
                defaults.setValue(info.loginName, forKey: "loginName")
                defaults.setValue(info.roleList, forKey: "roleList")
                defaults.setValue(info.deptList, forKey: "deptList")
                defaults.setValue(info.trueName, forKey: "trueName")
                defaults.setValue(info.headImg, forKey: "headImg")
                defaults.setValue(info.id, forKey: "id")
                defaults.setValue(info.email, forKey: "email")
                defaults.setValue(info.mobile, forKey: "mobile")
                defaults.setValue(info.userToken, forKey: "userToken")
                defaults .synchronize()
                
                //                SYUserInfo.strand.userName = model?.
                
                UserDefaults.standard.setValue(SYUserInfo.strand.userId, forKey: "userid")
                UserDefaults.standard.setValue(res.data?.userToken, forKey: "token")
                
                let tabbarVC = SYBaseTarBarViewController(roleId: "1")
                UIApplication.shared.keyWindow?.rootViewController = tabbarVC
            }
            
        } Error: {
            
        }
    }

}
